<template>
  <div class="NewsCard" @click="checkDetail">
    <div class="header">
      <img :src="cardInfo.bgUrl" alt />
    </div>
    <div class="content">
      <div class="top">
        <div class="t1">{{cardInfo.title}}</div>
        <div class="t2">{{formatContent(cardInfo.content)}}</div>
      </div>
      <div class="bottom">
        <div class="t2">发布时间:{{ cardInfo.add_time.slice(0,10) }}</div>
        <div class="t1" @click="checkDetail">查看更多→</div>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "NewsCard",
  props: {
    cardInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {};
  },
  mounted() {
    // console.log("cardInfo", this.cardInfo);

    this.initial();
  },
  methods: {
    async initial() {},
    formatContent(html) {
      var textContent = this.htmlToText(html);
      return textContent.slice(0, 50) + "...";
    },
    htmlToText(htmlString) {
      var tempDiv = document.createElement("div");
      tempDiv.innerHTML = htmlString;
      return tempDiv.textContent || tempDiv.innerText || "";
    },
    checkDetail() {
      sessionStorage.setItem('newsDetail', JSON.stringify(this.cardInfo));
      this.$router.push("/newsDetail");
      document.documentElement.scrollTop = 0;
    }
  }
};
</script>

<style lang="scss" scoped>
.NewsCard {
  height: 40vh;
  width: 45%;
  display: flex;
  flex-direction: column;
  // background: green;
  box-shadow: 0px 10px 40px 0px rgba(1, 51, 74, 0.15);
  border-radius: 5px;
  // margin-bottom: 40px;
  margin: 15px;
  overflow: hidden;

  .header {
    height: 40%;
    overflow: hidden;

    img {
      width: 100%;
    }
  }
  .content {
    width: 85%;
    flex: 1;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    .top {
      border-bottom: 1px solid #eeeeee;
      height: 75%;
      overflow: hidden;
      .t1 {
        margin-bottom: 10px;
        margin-top: 10px;
        font-family: Open Sans;
        font-weight: 600;
        font-size: 1.25em;
        line-height: 1.6;
        color: rgb(51, 51, 51);
        white-space: nowrap;
        overflow: hidden;
      }
      .t2 {
        color: #666666;
        margin-bottom: 15px;
      }
    }
    .bottom {
      height: 20%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .t2 {
        margin-top: 20px;
        font-size: 14px;
        color: #ababab;
      }
      .t1 {
        margin-top: 20px;
        cursor: pointer;
        color: $primaryColor;
      }
    }
  }

  @media screen and (max-width: 767px) {
    width: 100%;
    height: 50vh;
  }
}
</style>
